# coding: utf-8

"""
last edited at 2019-7-23
by Stone at BlueNet
公共函数库
"""
import cv2 as cv


# H, S for color recognition is enough
# HS_TH = ((15, 20), (1, 255))  # threshold of hue and saturation
# HS_LOWER = (10, 100)
# HS_UPPER = (20, 255)
HS_LOWER = (114, 100)
HS_UPPER = (206, 255)


def in_range_hsv(img_hsv, hsv_lower=HS_LOWER, hsv_upper=HS_UPPER):
    """
    基于HSV空间颜色阈值的图像二值化操作. 目前只用到H, S阈值, 而不使用V 的阈值,
    但是保留其位置以兼容未来的升级.

    :param img_hsv: the image in hsv color space to be threshold.
    :param hsv_lower: the lower threshold of hsv value. (currently using hs only)
    :param hsv_upper: the upper threshold of hsv value. (currently using hs only)
    :return: the bw image in np.uint8, the pixels in threshold is white, the pixels
        out of the threshold is black
    """
    img_wb = cv.bitwise_and(
        cv.inRange(img_hsv[:, :, 0], hsv_lower[0], hsv_upper[0]),
        cv.inRange(img_hsv[:, :, 1], hsv_lower[1], hsv_upper[1])
    )

    # debug
    # cv.imshow("bw", img_wb)

    return img_wb